package com.jueban.repository;

import com.jueban.model.ApplyOfIP;
import com.jueban.model.ApplyReward;
import com.jueban.model.user.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ApplyRewardRepository extends CrudRepository<ApplyReward, Integer> {


    @Query(value = "from ApplyReward as a join fetch a.applyOfIP " +
            "where a.user.department like %:department% " +
            "and (a.applyOfIP.finallyIPName like %:ipName% " +
            "or a.applyOfIP.applyInfo.applyIPName like %:ipName%) " +
            "and a.user.name like %:applicantName%", countQuery = "select count(a) from ApplyReward as a where a.user.department like %:department%" +
            " and (a.applyOfIP.finallyIPName like %:ipName%" +
            " or a.applyOfIP.applyInfo.applyIPName like %:ipName%)" +
            " and a.user.name like %:applicantName%")
    Page<ApplyReward> findApplyRewards(@Param("page") Pageable pageable
            , @Param("department") String department, @Param("ipName") String ipName
            , @Param("applicantName") String applicantName);


    @Query(value = "from ApplyReward as a join fetch a.applyOfIP where a.sate.hashCode = :state " +
            "and a.user.department like %:department% " +
            "and (a.applyOfIP.finallyIPName like %:ipName% " +
            "or a.applyOfIP.applyInfo.applyIPName like %:ipName%) " +
            "and a.user.name like %:applicantName%",countQuery = "select count(a) from ApplyReward as a where a.sate.hashCode = :state " +
            "and a.user.department like %:department% " +
            "and (a.applyOfIP.finallyIPName like %:ipName% " +
            "or a.applyOfIP.applyInfo.applyIPName like %:ipName%) " +
            "and a.user.name like %:applicantName%")
    Page<ApplyReward> findApplyRewardsWithState(@Param("page") Pageable pageable
            , @Param("state") int rewardState, @Param("department") String department
            , @Param("ipName") String ipName, @Param("applicantName") String applicantName);


    Page<ApplyReward> findApplyRewardsByUser(Pageable pageable, User user);

    ApplyReward findByApplyOfIP(ApplyOfIP applyOfIP);

}
